<?php session_start(); ?>
<?php include_once('misc/class_config.php'); ?>
<?php
	include_once('access_control/page_access.php');
	declinePageIfNotLoggedIn();
	declinePageIfNotAdmin();
	$currentUser= new user();
	$currentUser->isLoggedIn();
	$currentUser->isTimedOut();
?>


<?php
	include_once('model/category.php');

	$newProduct = new product();
	$productMessage = '';
	
	$productNameErrorMessage = '';
	$categoryErrorMessage = '';
	$unitPriceErrorMessage = '';
	$descriptionErrorMessage = '';
	$quantityErrorMessage = '';
	
	if(isset($_POST['btnAddProduct']))
	{
		$productValidationError = new validation();

		$productNameErrorMessage = $productValidationError->isEmpty(trim($_POST['name']),'<label class="required">First name is required.</label>');
		$categoryErrorMessage = $productValidationError->isListItemSelected(trim($_POST['category']),'<label class="required">Select a category.</label>');
		$unitPriceErrorMessage = $productValidationError->isEmpty(trim($_POST['price']),'<label class="required">Unit Price is required.</label>');
		$descriptionErrorMessage = $productValidationError->isEmpty(trim($_POST['description']),'<label class="required">Description is required.</label>');
		$quantityErrorMessage = $productValidationError->isEmpty(trim($_POST['quantity']),'<label class="required">Quantity is required.</label>');

		$newProduct->setProduct('',$_POST['name'],$_POST['price'],$_POST['description'],$_POST['quantity'],$_POST['category']);		
	}
?>

<?php
	if(isset($_POST['btnAddProduct']))
	{
		$formError = $productValidationError->status;
		
		if($formError==0)
		{
			try
			{
				if($newProduct->addProduct())
				{
					$productMessage = "Product added successfully.";
				}
				else
				{
					$productMessage = 'An error occured. Product was not added.';
				}
			}
			catch(Exception $ex)
			{
				$productMessage = $ex->getMessage();
			}
		}
	}
?>

<?php $pageTitle = "Add Product | Everything"; ?>
<?php include_once('layout/page_start.php'); ?>
<?php include_once('layout/page_header.php'); ?>
<?php include_once('layout/menu_bar.php'); ?>

<div id="main_content_div">
	<?php include("layout/configuration_menu_bar.php"); ?>
	<div id="add_product_form_div" class="form_div">
		<h2>Add Product</h2>
		<form action="addproduct.php" method="post">
			<table>
				<tr>
					<td><label>Product Name:</label></td> <td><input type="text" name="name" value="<?php if(isset($_POST['btnAddProduct'])){ echo $newProduct->name;  }?>"/> &nbsp &nbsp <?php echo $productNameErrorMessage; ?></td>
				</tr>
				<tr>
					<td><label>Category:</label></td> <td><select name ="category">
					<option selected name="default">Select Category</option>
					<?php
						try
						{
							$existingCategory = new category();
							$category = $existingCategory->getAllCategory();
							if($category)
							{
								foreach($category as $category)
								echo "<option value=".$category['code'].">".$category['name']."</option>";
							}
						}
						catch(Exception $ex)
						{
							echo  "Message: ".$ex->getmessage();					
						}	
					 ?> </select>  &nbsp &nbsp <?php echo $categoryErrorMessage; ?></td>
					
				</tr>
				<tr>
					<td><label>Unit Price:</label></td> <td><input type="text" name="price" value="<?php if(isset($_POST['btnAddProduct'])){ echo $newProduct->unitPrice; }?>"/>  &nbsp &nbsp <?php echo $unitPriceErrorMessage; ?></td> 
				</tr>
				<tr>
					<td><label>Description:</label></td> <td><input type="text" name="description" value="<?php if(isset($_POST['btnAddProduct'])){ echo $newProduct->description; }?>" />  &nbsp &nbsp <?php echo $descriptionErrorMessage; ?></td>
				</tr>
				<tr>
					<td><label>Quantity:</label></td> <td><input type="text" name="quantity" value="<?php if(isset($_POST['btnAddProduct'])){ echo $newProduct->onHand; }?>"/>  &nbsp &nbsp <?php echo $quantityErrorMessage; ?></td>
				</tr>
			</table>
			<input type="submit" name="btnAddProduct" value="Add Product"/>
			<input type="reset" name="btnReset" value="Reset Fields"/> <br/>
			
			<label><?php if(isset($productMessage)){echo $productMessage;} ?></label>
		</form>
	</div></br>
</div>
<?php include('layout/page_footer.php'); ?>
<?php include('layout/page_end.php'); ?>